/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.model;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Represents a given point in time. 
 * 
 * @author The Go-Getters Group
 * @version 1.0
 */


public class Calendar extends java.util.GregorianCalendar implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Constructs a new <code>Calendar</code> initialized with the current time.
	 */
	public Calendar(){
		super();
	}
	
	public Calendar(Calendar c){
		super(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH ), c.get(Calendar.HOUR_OF_DAY ),c.get(Calendar.MINUTE ));
	}
	
	public Calendar(int year, int month, int date, int hours, int minutes){
		super(year, month, date, hours, minutes);
	}
	
	public String toString(){
		Integer day, month, year;
		day = this.get(Calendar.DAY_OF_MONTH);
		month = this.get(Calendar.MONTH)+1;
		year = this.get(Calendar.YEAR);
		return day.toString()+"/"+month.toString()+"-"+year.toString();
	}
	
	
	public String getWeekDayString(){
		String[]weekDays = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
		return weekDays[this.get(DAY_OF_WEEK)-1];
	}
	
	/**
	 * Counts the days between this <code>Calendar</code> and another <code>Calendar</code> given as an argument.
	 */
	
	public int daysTo(Calendar date){
		Calendar first;
		Calendar second;
		int counter = 0;
		
		/* Decides which date is first and clones the earliest date, 
		 * so that it is not alteret, when it is counted up. 
		 */
		if(this.before(date)){
			first = (Calendar) this.clone();
			second = date;
		}
		else if(this.after(date)) {
			first = (Calendar) date.clone();
			second = this;
		}
		else
			return 0;
		
		/* This loop runs until the to dates match. For each run through the loop, 
		 * the counter is incremented by 1, while the first date is incremented by 1 day.
		 * The if-construct makes sure that the loop will no be infinite.
		 */ 
		while(!first.equalsDate(second)){
			first.add(Calendar.DATE,1);
			counter++;
			
		}
		
		//If the date given is earlier then this, the days should be negative.
		if(this.before(date))
			return counter;
		else
			return counter*-1;
		
	}
	
	public boolean isBetweenDate(Calendar startDate, Calendar endDate){
		if(startDate.before(this)&&endDate.after(this)){
			return true;
		}
		else if(startDate.equalsDate(this)||
				endDate.equalsDate(this)){
			return true;
		}
		return false;

	}
	
	public Calendar getFirstDayInWeek(){
		Calendar tempDate = new Calendar(this);
		while(tempDate.get(Calendar.DAY_OF_WEEK)!=Calendar.MONDAY){
			tempDate.add(Calendar.DAY_OF_WEEK, -1);
		}
		return tempDate;
	}
	
	public Calendar getLastDayInWeek(){
		Calendar tempDate = new Calendar(this);
		while(tempDate.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){
			tempDate.add(Calendar.DAY_OF_WEEK, 1);
		}
		return tempDate;
	}
	
	public ArrayList<Calendar> getDaysInWeek(){
		ArrayList<Calendar> daysInWeek = new ArrayList<Calendar>();
		Calendar tempDate=this.getFirstDayInWeek();
		for(int i = 1; i<8;i++){
			daysInWeek.add(new Calendar(tempDate));
			tempDate.add(Calendar.DAY_OF_WEEK, 1);
		}
		return daysInWeek;
	}
	
	public boolean equalsDate(Calendar date){
		if(this.get(Calendar.YEAR)==date.get(Calendar.YEAR)&&
				this.get(Calendar.MONTH)==date.get(Calendar.MONTH)&&
				this.get(Calendar.DATE)==date.get(Calendar.DATE)){
			return true;
		}
		return false;

	}
	
	public boolean isBetweenTime(Calendar startDate, Calendar endDate){
		if(startDate.before(this)&&endDate.after(this)){
			return true;
		}
		else if(startDate.equalsTime(this)||
				endDate.equalsTime(this)){
			return true;
		}
		return false;

	}
        
	public boolean equalsTime(Calendar date){
		if(this.get(Calendar.YEAR)==date.get(Calendar.YEAR)&&
				this.get(Calendar.MONTH)==date.get(Calendar.MONTH)&&
				this.get(Calendar.DATE)==date.get(Calendar.DATE)&&
				this.get(Calendar.HOUR_OF_DAY)==date.get(Calendar.HOUR_OF_DAY)&&
				this.get(Calendar.MINUTE)==date.get(Calendar.MINUTE)){
			return true;
		}
		return false;

	}
	
}
